package com.cg.service.impl;

import com.cg.dao.ArrangeMapper;
import com.cg.dao.DivideMapper;
import com.cg.dto.DateDto;
import com.cg.entity.Arrange;
import com.cg.entity.Divide;
import com.cg.service.ArrangeService;
import com.cg.utils.DateUtils;
import com.cg.vo.ArrangeDoctorVo;
import com.cg.vo.DateListVo;
import com.cg.vo.DateVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * (Arrange)表服务实现类
 *
 * @author chengong
 * @since 2024-01-08 12:02:04
 */
@Service("arrangeService")
public class ArrangeServiceImpl implements ArrangeService {
    @Resource
    private ArrangeMapper arrangeMapper;

    @Resource
    private DivideMapper divideMapper;

    /**
     * 预约-选择科室
     */
    @Override
    public DateListVo queryDateList(DateDto dateDto) throws ParseException {
       //返回的对象
        DateListVo dateListVo = new DateListVo();
        //查询科别名称
        Divide divide = divideMapper.queryDivideNameByDivideId(dateDto.getDivideId());
        //将科别名称放入返回的对象里面
        dateListVo.setDivideName(divide.getDivideName());

        //根据输入的日期区间获取对应的日期集合
        List<String> dayList = DateUtils.getDay(dateDto.getStartDate(), dateDto.getEndDate());
        if(dayList == null || dayList.size() == 0){
            return null;
        }
        //查询数据库里面的有余号的日期集合
        //根据日期区间和科别id查询

        List<DateVo> dateVoList = new ArrayList<>();

        List<Arrange> arrangeList = arrangeMapper.queryArrangeByDate(dateDto);
        if(arrangeList == null || arrangeList.size() == 0){
            //所有的日期都没有余号了
            for (String s : dayList) {
                //创建日期余号对象
                DateVo dateVo = new DateVo();
                dateVo.setDateDay(s);
                dateVoList.add(dateVo);
            }
            dateListVo.setDateVoList(dateVoList);
            return dateListVo;
        }
        //从数据库中查询到的日期有余号的时候
        for (String s : dayList) {
            DateVo dateVo = new DateVo();
            dateVo.setDateDay(s);
            for (Arrange arrange : arrangeList) {
                if(s.equals(arrange.getArrangeDate())){
                    //这一天有余号
                    //状态是1
                    dateVo.setIsCount(1);
                }
            }
            dateVoList.add(dateVo);
        }
        dateListVo.setDateVoList(dateVoList);
        return dateListVo;
    }

    /**
     * 根据divideId和时间查询医生信息
     */
    @Override
    public List<ArrangeDoctorVo> queryArrangeDoctor(DateDto dateDto) {
        return arrangeMapper.queryArrangeDoctor(dateDto);
    }
}
